
#include <msp430.h>

/*
 * ======== Grace related declaration ========
 */
extern void CSL_init(void);

/*
 *  ======== main ========
 */

unsigned char P1State;

int main(void)
{
    CSL_init();

	WDTCTL = WDTPW | WDTHOLD;

    P1State = 0x00;

    for(;;)
    {
    while (!(IFG2&UCA0TXIFG));
    __bis_SR_register(LPM0_bits + GIE);       // Enter LPM0, interrupts enabled
    }
}

void USCI0RX_ISR(void){}

void PORT1_ISR(void)
{
	__delay_cycles(10000);	//SW de-bounce

	//Check Pin 1.3
	if(P1IFG & BIT3 & ~P1State & ~P1IN)
	{
		//Zoom
		while (!(IFG2&UCA0TXIFG));
		UCA0TXBUF = 0x01;
		while (!(IFG2&UCA0TXIFG));
		UCA0TXBUF = 0xA4;
		while (!(IFG2&UCA0TXIFG));
		UCA0TXBUF = 0x00;
		while (!(IFG2&UCA0TXIFG));
		UCA0TXBUF = 0x5B;

		P1State |= BIT3;
	}
	else if(P1IFG & BIT3 & P1State & ~P1IN)
	{
		//Zoom
		while (!(IFG2&UCA0TXIFG));
		UCA0TXBUF = 0x01;
		while (!(IFG2&UCA0TXIFG));
		UCA0TXBUF = 0xA5;
		while (!(IFG2&UCA0TXIFG));
		UCA0TXBUF = 0x02;
		while (!(IFG2&UCA0TXIFG));
		UCA0TXBUF = 0x00;
		while (!(IFG2&UCA0TXIFG));
		UCA0TXBUF = 0x00;
		while (!(IFG2&UCA0TXIFG));
		UCA0TXBUF = 0x58;

		P1State &= ~BIT3;
	}

	//Check Pin 1.4
	if(P1IFG & BIT4 & ~P1State & ~P1IN)
	{
		//White hot
		while (!(IFG2&UCA0TXIFG));
		UCA0TXBUF = 0x01;
		while (!(IFG2&UCA0TXIFG));
		UCA0TXBUF = 0x29;
		while (!(IFG2&UCA0TXIFG));
		UCA0TXBUF = 0x00;
		while (!(IFG2&UCA0TXIFG));
		UCA0TXBUF = 0xD6;

		P1State |= BIT4;
	}
	else if(P1IFG & BIT4 & P1State & ~P1IN)
	{
		//Black hot
		while (!(IFG2&UCA0TXIFG));
		UCA0TXBUF = 0x01;
		while (!(IFG2&UCA0TXIFG));
		UCA0TXBUF = 0x28;
		while (!(IFG2&UCA0TXIFG));
		UCA0TXBUF = 0x00;
		while (!(IFG2&UCA0TXIFG));
		UCA0TXBUF = 0xD7;

		P1State &= ~BIT4;
	}

	__delay_cycles(10000);	//SW de-bounce

	P1IFG &= ~(BIT3 | BIT4);
}

